package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.CreateDepartment;
import library.and.xps.com.step.m.model.RequestParams;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseActivity;

public class CreateSubdepartmentActivity extends BaseActivity {
    TextView tv_title, tv_actionbar_right;
    EditText et_dep_name;
    String defaultDepartmentId;

    @Override
    protected int getLayout() {
        return R.layout.activity_subdepartment;
    }

    @Override
    protected void setData() {
        Intent intent = getIntent();
        defaultDepartmentId = intent.getStringExtra("defaultDepartmentId");
        if (TextUtils.isEmpty(defaultDepartmentId)) return;

    }

    @Override
    protected void init() {
        tv_title = r2v(R.id.actionbar_tv_title);
        tv_title.setText("创建子部门");
        et_dep_name = r2v(R.id.et_dep_name);
        tv_actionbar_right = r2v(R.id.actionbar_tv_right);
        tv_actionbar_right.setVisibility(View.VISIBLE);
        tv_actionbar_right.setText("创建");
        tv_actionbar_right.setTextColor(R.color.colorPrimary);
        tv_actionbar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ableCreate()) {
                    createDepartment();
                }
            }
        });
    }

    void createDepartment() {
        String json = RequestParams.getNewInstance()
                .setCreater(getUser().getUserid() + "")
                .setParentid(defaultDepartmentId)
                .setDname(et_dep_name.getText().toString())
                .setTeamid(getUser().getTeams().get(0).getTId())
                .getJson().toString();

        OkHttpClientUtils.postJsonStringAsync(this, URLConstants.TEAM.CreateDepartment, json, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
                dissmisDialog();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                String resJson = response.getBody();
                Logger.json(resJson);
                CreateDepartment createDepartment = new Gson().fromJson(resJson, CreateDepartment.class);
                if (HttpResponsHelper.SuccessResponce(createDepartment)) {
                    setResult(0);
                    finish();
                }
            }

            @Override
            public void onStart() {
                showDialog();
            }
        }, "");
    }

    boolean ableCreate() {
        if (et_dep_name.getText().toString().equals("")) {
            Snackbar.make(et_dep_name, "请输入部门名称", Snackbar.LENGTH_LONG)
                    .setAction("确定", null).show();
            return false;
        }
        return true;
    }

}
